package com.javatian.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.dao.CommentInfoDao;
import com.javatian.entity.Account;
import com.javatian.entity.CommentInfo;
import com.javatian.entity.GoodsInfo;
import com.javatian.service.*;
import exception.CustomException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品评价信息对外提供的服务实现类
 */
@Service("commentInfoService")
public class CommentInfoServiceImpl implements CommentInfoService {
    @Resource
    private CommentInfoDao commentInfoDao;

    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private WorkerInfoService workerInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    /**
     * 增加一条记录
     *
     * @param commentInfo
     */
    @Override
    public CommentInfo add(CommentInfo commentInfo) {
        commentInfo.setCreateTime(DateUtil.formatDateTime(new Date()));
        String content = commentInfo.getContent();
        if (content.length()>255){
            commentInfo.setContent(content.substring(0,250));
        }
        commentInfoDao.insertSelective(commentInfo);
        return commentInfo;
    }

    /**
     * 删除一条记录
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        commentInfoDao.deleteByPrimaryKey(id);

    }

    /**
     * 修改一条记录
     *
     * @param commentInfo
     */
    @Override
    public void update(CommentInfo commentInfo) {
        String content = commentInfo.getContent();
        if (content.length()>255){
            commentInfo.setContent(content.substring(0,250));
        }
        commentInfoDao.updateByPrimaryKeySelective(commentInfo);

    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public CommentInfo findById(Long id) {
        return commentInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有评价信息
     */
    @Override
    public List<CommentInfo> findAll() {
        return commentInfoDao.selectAll();
    }

    /**
     * 查询某个商品的评价信息
     *
     * @param goodsId
     */
    @Override
    public List<CommentInfo> findAll(Long goodsId) {
        List<CommentInfo> list = commentInfoDao.findByGoodsId(goodsId);
        if (!CollectionUtil.isEmpty(list)){
            setUserName(list);
        }
        return list;
    }


    /**
     * 根据条件分页查询
     *
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @param content  内容
     * @param user     用户信息
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<CommentInfo> findPage(Integer pageNum, Integer pageSize, String content, Account user) {
        PageHelper.startPage(pageNum,pageSize);
        if (user == null){
            throw new CustomException("1001","用户未登录");
        }
        List<CommentInfo> all;
        if (user.getLevel() == 1){
            all= commentInfoDao.selectAll();
        }else {
            all =commentInfoDao.findByContent(content,user.getId(),user.getLevel());
        }
        setUserName(all);
        return PageInfo.of(all);
    }

    /**
     * 给商品评价列表每一条记录设置用户名
     * @param list
     */
    private void setUserName(List<CommentInfo> list) {
        for (CommentInfo info:list) {
            Long userId = info.getUserId();
            Integer level = info.getLevel();
            if (level == 1){
                info.setUserName(adminInfoService.findById(userId).getName());
            }else if (level == 2){
                info.setUserName(workerInfoService.findById(userId).getName());
            }else if (level == 3){
                info.setUserName(userInfoService.findById(userId).getName());
            }
            GoodsInfo goodsInfo = goodsInfoService.findById(info.getGoodsId());
            info.setGoodsName(goodsInfo.getName());
        }
    }
}
